package day01;

import java.util.*;
import java.util.stream.Stream;
import java.util.stream.*;

import day01.Person;

public class StreamTest {
    public static void main(String[] args) {

//         findNumGt7();
//        voidTest2();
//        voidTest3();
//        test4();

        peepareData();
    }


    private static void peepareData() {
        List<Person> personList = new ArrayList<Person>();
        personList.add(new Person("Tom", 8900, 30, "male", "New York"));
        personList.add(new Person("Jack", 7000, 23, "male", "Washington"));
        personList.add(new Person("Lily", 7800, 33, "female", "Washington"));
        personList.add(new Person("Anni", 8200, 33, "female", "New York"));
        personList.add(new Person("Owen", 9500, 22, "male", "New York"));

        //案例三：获取员工工资最高的人

/*        //方式1
        personList.stream().max((a,b) -> a.getSalary()-b.getSalary()).ifPresent(System.out::println);
        //方式2
        personList.stream().max(Comparator.comparing(person -> person.getSalary())).ifPresent(System.out::println);
        //方式3
        personList.stream().max(Comparator.comparing(Person::getSalary)).ifPresent(System.out::println);*/
//        //案例4 计算员工工资大于7000人的个数
//        long count = personList.stream().filter(x -> {
//            return x.getSalary() > 7000;
//        }).count();
//        System.out.println("count = " + count);


        //  案例一：英文字符串数组的元素全部改为大写，整数数组每个元素+3
     /*   String[] strArr = { "abcd", "bcdd", "defde", "fTr" };
        List<Integer> intList = Arrays.asList(1, 3, 5, 7, 9, 11);


        List<String> strings = Arrays.asList(strArr);
        List<String> collect = strings.stream().map(x -> x.toUpperCase()).collect(Collectors.toList());
        System.out.println("转成大写之后 = " + collect);


        intList.stream().map(x -> x+3).forEach(System.out::println);*/
        //给所有员工工资+1000
//        personList.stream().map(person -> new Person(person.getName(), person.getSalary() + 1000, person.getAge(), person.getSex(), person.getArea())).collect(Collectors.toList()).forEach(System.out::println);
/*        //将两个字符数组合并成一个新的字符数组
        List<String> list = Arrays.asList("m,k,l,a", "1,3,5,7");
        list.stream().flatMap(x->Arrays.stream( x.split(","))).collect(Collectors.toList()).forEach(System.out::println);*/

/*


        //规约操作
        List<Integer> list = Arrays.asList(1, 3, 2, 8, 11, 4);


        //计算总和
        System.out.println(list.stream().reduce((a,b)-> a+b));

        // 求乘积

        list.stream().reduce((a,b)->a*b).ifPresent(System.out::println);

        // 求最大值方式1

        list.stream().reduce((a,b)-> a>b?a:b).ifPresent(System.out::println);


        list.stream().max(Comparator.comparing(a->a.intValue())).ifPresent(System.out::println);
*/


/*
        // 求总数
        long count = personList.stream().count();

        // 求平均工资

        Double average = personList.stream().collect(Collectors.averagingDouble(person -> person.getSalary()));

        // 求最高工资

        Integer max = personList.stream().map(x -> x.getSalary()).collect(Collectors.maxBy(Comparator.comparing(x -> x))).get();
        // 求工资之和

        Double sum = personList.stream().collect(Collectors.summingDouble(person -> person.getSalary()));*/

        // 一次性统计所有信息
  /*      DoubleSummaryStatistics collect = personList.stream().collect(Collectors.summarizingDouble(person -> person.getSalary()));

        System.out.println("员工总数：" + count);
        System.out.println("员工平均工资：" + average);
        System.out.println("员工最高工资：" + max);
        System.out.println("员工工资总和：" + sum);
        System.out.println("员工工资所有统计：" + collect);
*/

        //将员工按薪资是否高于8000分为两部分；将员工按性别和地区分组

        Map<Boolean, List<Person>> part = personList.stream().collect(Collectors.partitioningBy(person -> person.getSalary() > 8000));

/*        Map<String, Map<Integer, List<Person>>> group = personList.stream().collect(Collectors.groupingBy(Person::getSex, Collectors.groupingBy(person -> person.getAge())));
        Map<String, Map<String, List<Person>>> group2 = personList.stream().collect(Collectors.groupingBy(Person::getSex, Collectors.groupingBy(Person::getArea)));
//        System.out.println("员工按薪资是否大于8000分组情况：" + part);
//        System.out.println("员工按性别、地区：" + group2);
        //我要遍历map
        for (Map.Entry<String, Map<String, List<Person>>> entity :group2.entrySet()){
            Map<String, List<Person>> value = entity.getValue();
            for (Map.Entry<String,List<Person>> entry :value.entrySet()){
                List<Person> persons = entry.getValue();
                for (Person person:persons){
                    System.out.println("性别："+entity.getKey()+" 地区："+  entry.getKey()+ "person："+ person);
                }

            }
        }*/
        // 按工资升序排序（自然排序）
        List<Person> increaseOrder = personList.stream().sorted(Comparator.comparing(Person::getSalary)).collect(Collectors.toList());


        // 按工资倒序排序
        List<Person> decreaseOrder = personList.stream().sorted(Comparator.comparing(person -> person.getSalary(), Comparator.reverseOrder())).collect(Collectors.toList());


        // 先按工资再按年龄升序排序
        List<Person> inSalaryAndAge = personList.stream().sorted(Comparator.comparing(Person::getSalary).thenComparing(Person::getAge)).collect(Collectors.toList());


        // 先按工资再按年龄自定义排序（降序）
        List<Person> deSalaryAndAge = personList.stream().sorted(Comparator.comparing(Person::getSalary,Comparator.reverseOrder()).thenComparing(Person::getAge,Comparator.reverseOrder())).collect(Collectors.toList());

        //方式二
        //按照工资降序，按照年龄升序
        List<Person> collect = personList.stream().sorted((p1, p2) -> {
            if (p2.getSalary() == p1.getSalary()) {
                return p1.getAge() - p2.getAge();
            }
            //薪水降级
            return p2.getSalary() - p1.getSalary();
        }).collect(Collectors.toList());


    }

    //案例1 筛选集Integer集合总大7的数 并打印出来
    private static void findNumGt7() {
        Stream<Integer> integerStream = Stream.of(1, 214, 32, 13, 213, 13, 124, 2132, 3);
        integerStream.filter(num -> num > 7).forEach(System.out::println);
    }

    //案例2 选员工中工资高于8000的人，并形成新的集合。 形成新集合依赖collect（收集）
    private static void voidTest2() {
        List<Person> personList = new ArrayList<Person>();
        personList.add(new Person("Tom", 8900, 30, "male", "New York"));
        personList.add(new Person("Jack", 7000, 23, "male", "Washington"));
        personList.add(new Person("Lily", 7800, 33, "female", "Washington"));
        personList.add(new Person("Anni", 8200, 33, "female", "New York"));
        personList.add(new Person("Owen", 9500, 22, "male", "New York"));
        List<Person> result = personList.stream().filter(person -> person.getSalary() > 8000).collect(Collectors.toList());
        System.out.println(result);

    }

    //案例3 获取元素元素中最长的元素
    private static void voidTest3() {
        List<String> list = Arrays.asList("adnm", "admmt", "pot", "xbangd", "weoujgsd");
        Optional<String> result = list.stream().max(Comparator.comparing(String::length));
        if (result.isPresent()) {
            System.out.println(result.get());
        }

    }

    //案例4 获取Integer集合中的最大值
    private static void test4() {
        List<Integer> list = Arrays.asList(7, 6, 9, 4, 11, 6);

        list.stream().max(Comparator.comparing(Integer::valueOf)).ifPresent(System.out::println);
        list.stream().sorted((a, b) -> b - a).findFirst().ifPresent(System.out::println);


    }


}
